package com.zzyl.exception;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.base.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public ResponseResult<Object> handleBaseException(BaseException exception) {
        exception.printStackTrace();
        if (ObjectUtil.isNotEmpty(exception.getBasicEnum())) {
            log.error("自定义异常处理:{}", exception.getBasicEnum().getMsg());
        }
        return ResponseResult.error(exception.getBasicEnum());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult<String> handleException(RuntimeException e) {
        e.printStackTrace();
        if (ObjectUtil.isNotEmpty(e.getCause())) {
            log.error("系统异常:{}", e.getCause().getMessage());
        }
        log.error("系统异常:{}", e.getMessage());

        return ResponseResult.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<String> handleException(Exception e) {
        /*把错误信息打印在控制台*/
        e.printStackTrace();
        if (ObjectUtil.isNotEmpty(e.getCause())) {
            log.error("其他未知异常:{}", e.getMessage());
        }
        return ResponseResult.error(e.getMessage());
    }

}
